const Subscription = require('egg').Subscription;

const { createData3 } = require('../utils/tools')


// 复制所有任务生成3位值的后5位数据到新的表
class Copy2 extends Subscription{
    static get schedule(){
        return {
            interval: '24h',
            type: 'worker',
            // immediate: true,
            disable: true
        }
    }

    // 跑一次就行了
    async subscribe(){
        let { ctx } = this;
        const { Op } = ctx.model.Sequelize
        let data = await ctx.model.TotalEnter.findAll({
            where: { type: 2, task_id: { [Op.not]: null } }
        })
        for(let i=0;i<data.length;i++){
            let enter_id = data[i].dataValues.id
            console.log(enter_id)
            let detail = await ctx.model.TotalEnterDetail.findAll({ where: { enter_id, is_z: 0 } })
            let detailList = detail.slice(-5).reverse().map((item,index)=>{
                delete item.dataValues.id
                item.dataValues.index = index +1
                return item.dataValues
            })
            await ctx.model.TotalEnterDetail5.bulkCreate(detailList)
        }
    }
}

module.exports = Copy2